//委托构造就是在一个构造函数的初始化列表中调用另一个构造函数
//用于简化代码

#include<iostream>
#include<string>
using namespace std;

class AA
{
private:
	int m_a;
	int m_b;
	double  m_c;

public:
	AA(double c)	//有一个参数的构造函数，初始化m_c
	{
		m_c = c + 3;
		cout << "AA(double c)" << endl;
	}

	AA(int a, int b)	//有两个参数的构造函数，初始化m_a ,m_b
	{
		m_a = a + 1;
		m_b = b + 2;
		cout << "AA(int a, int b)" << endl;
	}

	/*
	AA(int a, int b, const string& str)		 
	{
		m_a = a + 1;
		m_b = b + 2;		//如果此m_a, m_b处初始化很复杂，这样写代码重复太多，不好
		cout << "m_a = " << m_a << " , m_b = " << m_b << ", str = " << str << endl;
	}
	*/
	AA(int a, int b, const string& str):AA(a, b)	//使用委托构造，效果相同
	{	
		//AA(a, b);	//不能在里面调用，这样只是创建匿名对象，不能初始化变量
		cout << "m_a = " << m_a << " , m_b = " << m_b << ", str = " << str << endl;
	}
	
	//同理
	AA(double c, const string& str) :AA(c)
	{
		cout << "m_c = " << m_c << ", str = " << str << endl;
	}
};

int main()
{
	AA a1(10, 9, "hello");
	AA a2(3.14, "world");
	return 0;
}

/*结果:
AA(int a, int b)
m_a = 11 , m_b = 11, str = hello
AA(double c)
m_c = 6.14, str = world
*/

//注意！
//1.不要生成环状的构造过程
//2.一旦使用委托构造，就不能在初始化列表中初始化其他的成员变量
			//例如：AA(double c, const string& str):AA(c), m_a(11){};	会报错







//继承构造
//在子类中用using来声明父类的构造函数
class BB
{
public:
	int m_a;
	int m_b;

	BB(int a) :m_a(a) 
	{ 
		cout << "BB(int a)" << endl;
	}
	BB(int a, int b) :m_a(a), m_b(b)
	{
		cout << "BB(int a, int b)" << endl;
	}
};

class CC : public BB
{
public:
	double m_c;
	using BB::BB;	//使用父类的构造函数，BB::BB前一个BB表示BB类，后一个BB表示BB类里的构造函数

	CC(int a, int b, double c) :BB(a, b), m_c(c)	//因为是调用的父类构造函数，所以不是委托构造
	{
		cout << "CC(int a, int b, double c)" << endl;
	}
	//此时子类CC中有三个构造函数
	void show()
	{
		cout << "m_a = " << m_a << " m_b = " << m_b << " m_c = " << m_c << endl << endl;
	}
};

int main()
{
	CC c1(10);
	c1.show();	//BB(int a)
				//m_a = 10 m_b = -858993460 m_c = -9.25596e+61
	CC c2(10, 20);
	c2.show();		//BB(int a, int b)
					//m_a = 10 m_b = 20 m_c = -9.25596e+61

	CC c3(10, 20, 10.12);
	c3.show();		//BB(int a, int b)
					//CC(int a, int b, double c)
					//m_a = 10 m_b = 20 m_c = 10.12
	return 0;


}
		


		
